package com.alex.common.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alex.common.Constants;
import com.alex.common.exception.AlexCommException;
import com.alex.common.persistent.UserDao;
import com.alex.common.security.domain.User;
import com.alex.common.tools.CookieUtils;
import com.alex.common.tools.HostUtils;
import com.alex.common.tools.PropertyUtils;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserDao userDao;

	private static final String Domain = PropertyUtils.getProperty("domain");

	/**
	 * Used by real_host, like "alexa.sinaapp.com/user" set cookie to "/"
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody
	String index(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		if (RequestMethod.GET.name().equals(request.getMethod())) {
			// 1st request
			// get is from A domain to B domain, so set Access-Control-Allow
			response.addHeader("Access-Control-Allow-Origin", "*");
			response.addHeader("Access-Control-Allow-Methods", "POST,GET");
			response.addHeader("Access-Control-Allow-Credentials", "true");
			// set over
			String ipaddr = HostUtils.getRemoteAddr(request);
			// for ajax post
			return ipaddr;
		}

		if (RequestMethod.POST.name().equals(request.getMethod())) {
			// 2st request
			CookieUtils cu = new CookieUtils(request, response);
			Cookie ck = cu.get(Constants.COOKIE_NAME_CLIENTSIGN);
			if (ck == null) {
				String remoteAddr = request.getParameter("clientsign");
				ck = cu.create(Constants.COOKIE_NAME_CLIENTSIGN, remoteAddr,
						600);// 10 min
				ck.setDomain("." + Domain);
				cu.add(ck);
				return remoteAddr;// for debug only
			} else {
				return ck.getValue();// for debug only
			}
		}

		throw new AlexCommException(new IllegalStateException(
				"Not get, not post, do what?"));
	}

	@RequestMapping(value = "init", method = { RequestMethod.GET })
	public @ResponseBody
	String init() {
		User zhaotianba = new User(1L, "zhaotianba",
				"25d55ad283aa400af464c76d713c07ad", "天霸 赵", "",
				User.Authority.ADMIN.ordinal(), new Date(1352706963104L),
				new Date());
		User zhangsam = new User(2L, "zhangsam",
				"25d55ad283aa400af464c76d713c07ad", "Sam 张", "",
				User.Authority.USER.ordinal(), new Date(1352706963104L),
				new Date());

		String rt = "init ";
		if (userDao.find(zhaotianba.getUsername()) == null) {
			userDao.save(zhaotianba);
			rt = rt + zhaotianba.getUsername() + " ";
		}
		if (userDao.find(zhangsam.getUsername()) == null) {
			userDao.save(zhangsam);
			rt = rt + zhangsam.getUsername() + " ";
		}
		return rt + "OK.";
	}
}
